<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    *{
      margin: 0;
      padding: 0;
    }
    img{
      height: 400px;
      width: 400px;
    }
  </style>
</head>
<body >
  <div style="height: 2000px;">
    <img src="https://cdn.pixabay.com/photo/2021/06/15/13/20/banded-demoiselle-6338620__340.jpg" alt="">
    <img src="https://cdn.pixabay.com/photo/2021/05/15/00/42/street-6254667__340.jpg" alt="">
    <img src="https://cdn.pixabay.com/photo/2021/06/10/22/43/jellyfish-6327182__340.jpg" alt="">
    <img src="https://cdn.pixabay.com/photo/2021/05/23/17/52/bellies-6276856__340.jpg" alt="">
    <img src="https://cdn.pixabay.com/photo/2021/05/14/12/26/man-6253257__340.jpg" alt="">
    <img src="https://cdn.pixabay.com/photo/2021/05/24/12/17/rocks-6278936__340.jpg" alt="">
    <img src="https://cdn.pixabay.com/photo/2018/04/05/15/01/nature-3293045__340.jpg" alt="">
    <img src="https://cdn.pixabay.com/photo/2021/04/27/13/32/birds-6211541__340.png" alt="">
    <img src="https://cdn.pixabay.com/photo/2021/06/15/13/20/banded-demoiselle-6338620__340.jpg" alt="">
    <img src="https://cdn.pixabay.com/photo/2021/05/15/00/42/street-6254667__340.jpg" alt="">
    <img src="https://cdn.pixabay.com/photo/2021/06/10/22/43/jellyfish-6327182__340.jpg" alt="">
    <img src="https://cdn.pixabay.com/photo/2021/05/23/17/52/bellies-6276856__340.jpg" alt="">
    <img src="https://cdn.pixabay.com/photo/2021/05/14/12/26/man-6253257__340.jpg" alt="">
    <img src="https://cdn.pixabay.com/photo/2021/05/24/12/17/rocks-6278936__340.jpg" alt="">
    <img src="https://cdn.pixabay.com/photo/2018/04/05/15/01/nature-3293045__340.jpg" alt="">
    <img src="https://cdn.pixabay.com/photo/2021/04/27/13/32/birds-6211541__340.png" alt="">
    <img src="https://cdn.pixabay.com/photo/2021/06/15/13/20/banded-demoiselle-6338620__340.jpg" alt="">
    <img src="https://cdn.pixabay.com/photo/2021/05/15/00/42/street-6254667__340.jpg" alt="">
    <img src="https://cdn.pixabay.com/photo/2021/06/10/22/43/jellyfish-6327182__340.jpg" alt="">
    <img src="https://cdn.pixabay.com/photo/2021/05/23/17/52/bellies-6276856__340.jpg" alt="">
    <img src="https://cdn.pixabay.com/photo/2021/05/14/12/26/man-6253257__340.jpg" alt="">
    <img src="https://cdn.pixabay.com/photo/2021/05/24/12/17/rocks-6278936__340.jpg" alt="">
    <img src="https://cdn.pixabay.com/photo/2018/04/05/15/01/nature-3293045__340.jpg" alt="">
    <img src="https://cdn.pixabay.com/photo/2021/04/27/13/32/birds-6211541__340.png" alt="">
  </div>
  <script>
    // let div = document.getElementById("div")
    // console.log(div.getBoundingClientRect())

    var imgs = [...document.querySelectorAll('img')] //转为数组
    let deleteImgs = [] 
    /**
     * 懒加载函数
     * img有src属性才会加载图片
     * 判断图片是否进入视口，进入则为img添加src属性
     */
    function imgLazyLoad(){
      console.log("事件")
      let reloadHeight = 500 // 预加载高度
      imgs.forEach((img,index) => {
        let isIntoTop = 0-img.getBoundingClientRect().height-reloadHeight < img.getBoundingClientRect().top //图片底部进入预加载区域
        let isIntoBottom = img.getBoundingClientRect().top < window.innerHeight+reloadHeight // 图片顶部进入预加载区域
        if(isIntoTop && isIntoBottom && !img.src){
          img.src = img.dataset.src
          // 记录要删除的img
          deleteImgs.push(index)
        }
      });
      // 已加载的图片移除，下次只循环没有加载的图片
      imgs = imgs.filter((img,index)=>!deleteImgs.includes(index))
      deleteImgs = [] //重置
      // 所有图片加载完成移除监听事件
      if(!imgs.length){
        console.log("移除")
        document.removeEventListener("scroll",scrollFunction)
      }
    }
    imgLazyLoad()
    // 添加 滚动监听 事件
    // document.addEventListener("scroll",debounce(imgLazyLoad,50)) // 直接使用addEventListener 函数传参会无法移除事件
    let scrollFunction = addEventListenerHandler("scroll",debounce(imgLazyLoad,50))
    /**
     * 防抖函数
     * fn: 需要防抖的函数名
     * delay: 延迟毫秒数
     * args: 传给fn的参数
     */
    function debounce(fn,delay,...args){
      let timer = null
      return function(){
        if(timer){
          clearTimeout(timer)
        }
        timer = setTimeout(() => {
          fn(...args)

        }, delay);
      }
    }
    /**
     * 工具函数，给绑定监听事件 传参 ，
     *  eventName: 监听事件 类型
     *  fn: 事件名
     *  ...args: 剩余参数，传个fn的参数
     * return 监听绑定的 函数 ，用于取消监听事件
     */
    function addEventListenerHandler(eventName, fn, ...args){
      let scrollFunction = function(){
        fn.call(this, fn(...args))
      }
      document.addEventListener(eventName,scrollFunction)
      return scrollFunction
    }
  </script>
</body>
</html>